package com.mydemo.netty.base.handler;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.nio.charset.StandardCharsets;
import java.util.List;

public class MyInHandler1 extends ByteToMessageDecoder {

    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        if (byteBuf.readableBytes() < 1) {
            return;
        }
        byte[] bytes = new byte[1];
        byteBuf.readBytes(bytes, 0, 1);
        String result = new String(bytes, StandardCharsets.UTF_8);
        System.out.println("MyInHandler1读取到客户端的数据=" + result);
        list.add(result);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("发生异常=" + cause.getMessage());
    }
}
